/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import WebFeeds 1.0
import NewsBlur 1.0
import "../../../../shared/qml" as Shared
import "../../../../shared/qml/UIConstants.js" as UIConstants
import "../delegates" as Delegates
import "../misc" as Misc
import "../buttons" as Buttons

NewsBlurPage {
    id: root

    Component {
        id: feedDelegate
        Delegates.FeedPageListDelegate {
            onReleased: createPageForItem(listPageContents.itemForRow(row));
            onHolded: showContextMenuForItem(listPageContents.itemForRow(row));
        }
    }

    Component {
        id: starredAndEverythingDelegate
        Delegates.StarredPageListDelegate {
            service: root.rootItem.service;
            onReleased: {
                if(!root.rootItem.movingBusy && !root.rootItem.deletingBusy) {
                    var item = webFeedsApp.pageItemModel.item(listPageContents.indexForRow(row));
                    createPageForItem(listPageContents.itemForRow(row));
                }
            }
            onHolded: {
                if(!root.rootItem.movingBusy && !root.rootItem.deletingBusy)
                    showContextMenuForItem(listPageContents.itemForRow(row));
            }
        }
    }

    SelectionDialog {
        id: filterSelectionDialog
        titleText: qsTr("Show me")
        model: ListModel {            
            ListElement {name: "Everything"; icon: "qrc:/plugins/newsblur/images/count_bubble/no_filter.png"}
            ListElement {name: "Everything I don't dislike"; icon: "qrc:/plugins/newsblur/images/count_bubble/non_negative_filter.png"}
            ListElement {name: "Just the stuff I like"; icon: "qrc:/plugins/newsblur/images/count_bubble/positive_filter.png"}
        }
        delegate: Qt.createComponent("/shared/qml/SelectionDialogIconDelegate.qml");
        onStatusChanged: {
            if((filterSelectionDialog.status == DialogStatus.Opening)
               || (filterSelectionDialog.status == DialogStatus.Open)) {
                filterSelectionDialog.selectedIndex = root.service.filterType;
            }
        }
        onAccepted: {
            if(filterSelectionDialog.selectedIndex >= 0) {
                root.service.setFilterType(filterSelectionDialog.selectedIndex,
                                      root.rootItem);
            }
        }        
    }

    contents: Shared.CommonListPageContents {
        id: listPageContents
        rootItem: root.rootItem
        rootIndex: webFeedsApp.pageItemModel.index(root.rootItem);
        busy: root.contentsBusy        
        emptyListText: qsTr("No stories")
        pageHeader: Shared.CommonPageHeader {
            filterEnabled: (root.rootItem.itemType != NewsBlurItem.StarredGroup);
            onClicked: filterSelectionDialog.open();
            titleText: root.rootItem.title
            titleColor: "white"
            backgrounColor: UIConstants.HIGHLIGHT_COLOR
            titleFont: UIConstants.FONT_FAMILY_LIGHT
            rightHelperItem: Misc.HeaderCountBubble {
                busy: root.rootItem.busy
                neutralCount: root.rootItem.unreadCount
                negativeCount: root.rootItem.negUnreadCount
                positiveCount: root.rootItem.posUnreadCount
                filterType: root.service.filterType
                hideNullSegments: (root.rootItem.itemType == NewsBlurItem.StarredGroup)
            }
            Shared.Separator {
                id: separator
                anchors.bottom: parent.bottom
            }
        }
        delegate: (root.rootItem.itemType == NewsBlurItem.FeedItem) ? feedDelegate : starredAndEverythingDelegate
    }
    tools: Shared.CommonToolBarLayout {
        enabled: root.userInteractionEnabled
        Shared.BackToolButton {pageItem: root.rootItem; page: root}
        Buttons.MarkFeedAsReadToolButton {pageItem: root.rootItem}
        Buttons.MarkEverythingAsReadToolButton {pageItem: root.rootItem}
        Buttons.RefreshEverythingGoupToolButton {pageItem: root.rootItem}
        Shared.MenuToolButton {pageItem: root.rootItem; page: root}
    }
}
